/*
 * @Author: your name
 * @Date: 2021-04-14 16:03:45
 * @LastEditTime: 2021-09-30 17:56:54
 * @LastEditors: LDM
 * @Description: In User Settings Edit
 * @FilePath: \vue3-composition-admin-main\src\axios\request.ts
 */
import { MAxios } from './myAxios'
// import type { AxiosRequestConfig, ResponseType } from 'axios'
import { useUserStoreWithOut } from '/@/store'
// import { ElMessage } from 'element-plus'
// import { router } from '/@/router'
export type { Result } from './myAxios'

const axios = new MAxios({
  baseURL: process.env.BASE_API,
  needToken: true, //是否添加bearer
  headers: {}
})
/* 请求前拦截 */
axios.beforRequest = function (config) {
  const userStore = useUserStoreWithOut()
  // do something before  request.....
  if (config.needToken) {
    config.headers.Authorization = 'Bearer ' + userStore.$state.token
  }
  return config
}
/* 请求后拦截 */
axios.afterRequest = function (res) {
  const result = res.data

  // 这里可以做拦截判断是否错误
  /* if (result.code !== 10000) {
    ElMessage.error(result.msg)
    //登录失效
    if (result.code === 10401) {
      //如果出现错误,或登录失效, 可以进行重新登录

      router.replace({ path: '/login' })
    }
    return Promise.reject(result)
  } */

  return result
}
/* 请求后错误拦截 */
//eslint-disable-next-line
axios.afterRequestError = function (_err) {
  // TODO 如果请求拦截后错误
  // const errorMsg = (err.response && err.response?.data.msg) || err.message

  return Promise.reject()
}
export default axios
